package com.heima.file.service.impl;


import com.aliyun.oss.OSS;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectRequest;
import com.heima.file.config.MinIOConfig;
import com.heima.file.config.MinIOConfigProperties;
import com.heima.file.config.OssConfig;
import com.heima.file.config.OssConfigProperties;
import com.heima.file.service.FileStorageService;
import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Import;
import org.springframework.util.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
//开启配置属性类
@EnableConfigurationProperties(OssConfigProperties.class)
@Import(OssConfig.class)
public class OSSFileStorageService implements FileStorageService {

    @Autowired
    private OSS oss;

    @Autowired
    private OssConfigProperties ossConfigProperties;


    /**
     *  上传图片文件
     * @param prefix  文件前缀
     * @param filename  文件名
     * @param inputStream 文件流
     * @return  文件全路径
     */
    @Override
    public String uploadImgFile(String prefix, String filename,InputStream inputStream) {
        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest(ossConfigProperties.getBucket()
                , filename,
                inputStream);

        // 上传字符串。
        oss.putObject(putObjectRequest);

        return ossConfigProperties.getReadPath() + filename;
    }

    /**
     *  上传html文件
     * @param prefix  文件前缀
     * @param filename   文件名
     * @param inputStream  文件流
     * @return  文件全路径
     */
    @Override
    public String uploadHtmlFile(String prefix, String filename,InputStream inputStream) {
        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest(ossConfigProperties.getBucket()
                , filename,
                inputStream);

        // 上传字符串。
        oss.putObject(putObjectRequest);

        return ossConfigProperties.getReadPath() + filename;
    }

    /**
     * 删除文件
     * @param pathUrl  文件全路径 https://cloudprogrammer.cn/2.jpg
     */
    @Override
    public void delete(String pathUrl) {
        String fileName = pathUrl.replace(ossConfigProperties.getReadPath(), "");
        oss.deleteObject(ossConfigProperties.getBucket(),fileName);
    }


    /**
     * 下载文件
     * @param pathUrl  文件全路径
     * @return  文件流
     *
     */
    @Override
    public byte[] downLoadFile(String pathUrl)  {
        String fileName = pathUrl.replace(ossConfigProperties.getReadPath(), "");
        OSSObject object = oss.getObject(ossConfigProperties.getBucket(), fileName);

        InputStream inputStream = null;
        try {
            inputStream = object.getObjectContent();
        } catch (Exception e) {
            log.error("minio down file error.  pathUrl:{}",pathUrl);
            e.printStackTrace();
        }

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100];
        int rc = 0;
        while (true) {
            try {
                if (!((rc = inputStream.read(buff, 0, 100)) > 0)) break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            byteArrayOutputStream.write(buff, 0, rc);
        }
        if(inputStream != null){
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return byteArrayOutputStream.toByteArray();
    }
}
